package com.sulent.frame.web.config;

import org.apache.commons.net.ftp.FTPFile;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.file.remote.session.SessionFactory;
import org.springframework.integration.ftp.gateway.FtpOutboundGateway;
import org.springframework.integration.ftp.session.DefaultFtpSessionFactory;
import org.springframework.integration.ftp.session.FtpRemoteFileTemplate;
import org.springframework.integration.ftp.session.FtpSession;
import org.springframework.messaging.MessageHandler;


//@Configuration
public class FtpConfig {

    @Bean
    public DefaultFtpSessionFactory ftpSessionFactory() {
        DefaultFtpSessionFactory factory = new DefaultFtpSessionFactory();
        factory.setHost("192.168.138.100");
        factory.setPort(21);
        factory.setUsername("ftpuser");
        factory.setPassword("Su8023@bb");
        return factory;
    }
/*
    @Bean
    public SessionFactory<FTPFile> ftpSessionFactory() {
        DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory();
        sf.setHost("localhost");
        sf.setPort(port);
        sf.setUsername("foo");
        sf.setPassword("foo");
        sf.setTestSession(true);
        return new CachingSessionFactory<FTPFile>(sf);
    }*/


    @Bean
    public FtpRemoteFileTemplate ftpRemoteFileTemplate(DefaultFtpSessionFactory defaultFtpSessionFactory){
        FtpSession session = defaultFtpSessionFactory.getSession();
        return new FtpRemoteFileTemplate(defaultFtpSessionFactory);
    }


    @Bean
    @ServiceActivator(inputChannel = "ftpChannel")
    public MessageHandler ftpOutboundGateway(DefaultFtpSessionFactory defaultFtpSessionFactory) {
        return new FtpOutboundGateway(defaultFtpSessionFactory,
                (session, requestMessage) -> session.list(String.valueOf(requestMessage.getPayload())));
    }

}